<?php
include_once 'field.class.php';

class field_textarea extends field
{
    public $error;
    protected $fieldname = '多行文本';

    /**
     * 生成设置项
     * @author menghao
     * @param <string> $data
     * @return <string>
     */
    function settings($data='')
    {
        $data = unserialize($data);
        $data['wsize'] = isset($data['wsize']) && is_numeric ($data['wsize']) ? $data['wsize'] : 50;
        $data['hsize'] = isset($data['hsize']) && is_numeric ($data['hsize']) ? $data['hsize'] : 6;
        $data['minlength'] = isset($data['minlength']) && is_numeric ($data['minlength']) ? $data['minlength'] : 0;
        $data['maxlength'] = isset($data['maxlength']) && is_numeric ($data['maxlength']) ? $data['maxlength'] : 200;

        $setting = '';
        $setting .= '<label>文本框宽度：'.form::text('settings[wsize]', 'setting_wsize', $data['wsize'], 7).'</label> ';
        $setting .= '<label>文本框高度：'.form::text('settings[hsize]', 'setting_hsize', $data['hsize'], 7).'</label> ';
        $setting .= '<label>文本最小长度(字节)：'.form::text('settings[minlength]', 'setting_minlength', $data['minlength'], 7).'</label> ';
        $setting .= '<label>文本最大长度(字节)：'.form::text('settings[maxlength]', 'setting_maxlength', $data['maxlength'], 7).'</label> ';

        return $setting;
    }


    /**
     * 检查设置项数据
     * @author menghao
     * @param <array> $data
     */
    function check_settings($data)
    {
        if(!is_numeric($data['wsize']))
        {
            $this->error = "文本框宽度必须是数字";
            return false;
        }
        if(!is_numeric($data['hsize']))
        {
            $this->error = "文本框高度必须是数字";
            return false;
        }
        if(!is_numeric($data['minlength']))
        {
            $this->error = "内容最小长度必须是数字";
            return false;
        }
        if(!is_numeric($data['maxlength']))
        {
            $this->error = "内容最大长度必须是数字";
            return false;
        }
        return true;
    }

    function check_data($data)
    {
        if(strlen($data) < $this->field_data['settings']['minlength'])
        {
            $this->error = "{$this->field_data['name']}最小长度不能少于 {$this->field_data['settings']['minlength']}字节！";
            $this->response->text_alert('error', $this->field_data['field'], $this->error);
            return false;
        }
        elseif(strlen($data) > $this->field_data['settings']['maxlength'])
        {
            $this->error = "{$this->field_data['name']}最大长度不能超过 {$this->field_data['settings']['maxlength']}字节！";
            $this->response->text_alert('error', $this->field_data['field'], $this->error);
            return false;
        }
        return parent::check_data($data);
    }

    function create_form($value='')
    {
        $field = $this->field_data['field'];
        $wsize = $this->field_data['settings']['wsize'];
        $hsize = $this->field_data['settings']['hsize'];
        $form = form::textarea("data[$field]", $field, $value, $wsize, $hsize);
        return $form;
    }
}
?>